package com.loveyukari.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.stereotype.Repository;

import com.loveyukari.dao.VoteDao;
import com.loveyukari.model.VoteGroup;
import com.loveyukari.model.VoteItem;

/**
*
*yangshaohua
*2017年5月20日下午5:55:13
**/

@Repository
public class JdbcVoteDao extends BaseDao implements VoteDao{

	private class VoteGroupRowMapper implements RowMapper<VoteGroup>{

		public VoteGroup mapRow(ResultSet rs, int rowNum) throws SQLException {
			VoteGroup group = new VoteGroup();
			group.setId(rs.getInt("id"));
			group.setStartTime(rs.getTimestamp("start_time"));
			group.setEndTime(rs.getTimestamp("end_time"));
			group.setStatus(rs.getInt("status"));
			group.setName(rs.getString("name"));
			return group;
		}
		
	}
	
	private class VoteItemRowMapper implements RowMapper<VoteItem>{

		public VoteItem mapRow(ResultSet rs, int rowNum) throws SQLException {
			VoteItem item = new VoteItem();
			item.setId(rs.getInt("id"));
			item.setGroupId(rs.getInt("group_id"));
			item.setCount(rs.getInt("count"));
			item.setNote(rs.getString("note"));
			item.setKey(rs.getString("vote_key"));
			return item;
		}
		
	}
	
	private class VoteItemDetailRowMapper extends VoteItemRowMapper{
	
	@Override
	public VoteItem mapRow(ResultSet rs, int rowNum) throws SQLException {
		VoteItem item = super.mapRow(rs, rowNum);
		item.setAuthIds(rs.getString("auth_ids"));
		return item;
	}
		
	}
	public Integer insertVoteGroup(final String name) {
		final String sql = "insert into vote_group(name,start_time) values(?,now())";
		GeneratedKeyHolder keyHolder = new GeneratedKeyHolder();
		this.getJdbcTemplate().update(new PreparedStatementCreator() {
			
			public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
				PreparedStatement pstm = con.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
				pstm.setString(1, name);
				return pstm;
			}
		},keyHolder);
		return keyHolder.getKey().intValue();
	}

	public VoteGroup selectLastVoteGroup() {
		String sql = "select * from vote_group where status = 0 order by id desc limit 1";
		List<VoteGroup> list = this.getJdbcTemplate().query(sql, new VoteGroupRowMapper());
		if (list.size() == 1){
			return list.get(0);
		}
		return null;
	}

	public List<VoteItem> selectVoteItemByGroup(int groupId) {
		String sql = "select * from vote_item where group_id = ?";
		List<VoteItem> list = this.getJdbcTemplate().query(sql, new VoteItemRowMapper(),groupId);
		return list;
	}

	public VoteItem selectVoteItemById(int id) {
		String sql = "select * from vote_item where id = ?";
		List<VoteItem> list = this.getJdbcTemplate().query(sql, new VoteItemDetailRowMapper(),id);
		if (list.size() == 1){
			return list.get(0);
		}
		return null;
	}

	public void insertVoteItem(VoteItem voteItem) {
		String sql = "insert into vote_item (group_id,vote_key,note) values(?,?,?)";
		this.getJdbcTemplate().update(sql,voteItem.getGroupId(),voteItem.getKey(),voteItem.getNote());
	}

	public void updateVoteItem(VoteItem voteItem) {
		String sql = "update vote_item set count=?,auth_ids=? where id=?";
		this.getJdbcTemplate().update(sql,voteItem.getCount(),voteItem.getAuthIds(),voteItem.getId());
		
	}

	public void updateVoteGroup(VoteGroup voteGroup) {
		String sql = "update vote_group set end_time = ?,status=? where id = ?";
		this.getJdbcTemplate().update(sql,voteGroup.getEndTime(),voteGroup.getStatus(),voteGroup.getId());
		
	}

	public VoteGroup selectVoteGroupById(int id) {
		String sql = "select * from vote_group where id = ?";
		List<VoteGroup> list = this.getJdbcTemplate().query(sql, new VoteGroupRowMapper(),id);
		if (list.size() == 1){
			return list.get(0);
		}
		return null;
	}

	public List<VoteGroup> selectVoteGroupAll() {
		String sql = "select * from vote_group order by id desc";
		List<VoteGroup> list = this.getJdbcTemplate().query(sql, new VoteGroupRowMapper());
		return list;
	}

}
